package com.zh.api.controller.operation;
import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.domain.DictDO;
import com.zh.common.utils.*;
import com.zh.operation.domain.OperationGoodsDO;
import com.zh.operation.domain.OperationGoodsDOVO;
import com.zh.operation.service.OperationGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-01-17 15:36:38
 */
 
@Controller
@Api("资产【养老院物品】")
public class OperationGoodsController extends BaseController {
	@Autowired
	private OperationGoodsService operationGoodsService;

	@Log("养老院物品列表")
	@ResponseBody
	@RequestMapping(value = "listOperationGoods", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationGoods(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<OperationGoodsDOVO> goodsList = operationGoodsService.list(query);
		int total = operationGoodsService.count(query);
		PageUtils pageUtils = new PageUtils(goodsList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑物品")
	@ResponseBody
	@RequestMapping(value = "editOperationGoodsById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationGoodsBuyById(@RequestBody OperationGoodsDO goods){
		if(goods==null || CheckEmptyUtil.isEmpty(goods.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		OperationGoodsDO operationGoodsDO = operationGoodsService.get(goods.getId());
		return ResultVO.success(operationGoodsDO);
	}



	@Log("查询字典表中的物品类型")
	@ResponseBody
	@RequestMapping(value = "listOperationGoodsType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationGoodsType(){
		List<DictDO> list = operationGoodsService.listGoodsType();
		return ResultVO.success(list);
	}

	@Log("新增物品【只能添加新物品】")
	@ResponseBody
	@RequestMapping(value = "saveOperationGoods", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationGoodsBuy(@RequestBody OperationGoodsDO goods){
		if(goods==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		OperationGoodsDO operationGoodsDO = operationGoodsService.isExist(goods.getGoodsName());
		if(operationGoodsDO != null){
			return ResultVO.error();
		} else{
			goods.setCreateBy(getUserId());
			operationGoodsService.save(goods);
			return ResultVO.success();
		}
	}


	@Log("更新物品信息")
	@ResponseBody
	@RequestMapping(value = "updateOperationGoodsById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationGoodsBuyById(@RequestBody OperationGoodsDO goods){
		if(goods==null || CheckEmptyUtil.isEmpty(goods.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		operationGoodsService.update(goods);
		return ResultVO.success();
	}

	@Log("删除物品")
	@ResponseBody
	@RequestMapping(value = "removeOperationGoodsById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationGoodsBuyById(@RequestBody OperationGoodsDO goods){
		if(goods==null || CheckEmptyUtil.isEmpty(goods.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(operationGoodsService.remove(goods.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@RequestMapping(value = "batchRemoveOperationGoodsByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除物品")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(operationGoodsService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
}
